package com.example.demo.domain;

public class ProjectConfig {

    private String databaseType;

    private String host;

    private String port;

    private String databaseName;

    private String username;

    private String password;

    private String url;

    private String driverClassName;

    private Integer maxActive = 200;

    private Integer minIdle = 20;

    private Integer initialSize = 20;

    private Long maxWait = 6000L;

    private Long timeBetweenEvictionRunsMillis = 60000L;

    private Long minEvictableIdleTimeMillis = 300000L;

    private Boolean testWhileIdle = true;

    private Boolean testOnBorrow = false;

    private Boolean testOnReturn = false;

    private String validationQuery;

    private Boolean poolPreparedStatements = true;

    private Integer maxPoolPreparedStatementPerConnectionSize = 20;

    public String getDatabaseType() {
        return databaseType;
    }

    public void setDatabaseType(String databaseType) {
        this.databaseType = databaseType;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getDatabaseName() {
        return databaseName;
    }

    public void setDatabaseName(String databaseName) {
        this.databaseName = databaseName;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
        //数据库连接信息password是最后一个定义，所以把初始化方法加到这里。
        init();
    }

    public String getDriverClassName() {
        return this.driverClassName;
    }

    public String getUrl() {
        return this.url;
    }

    public Integer getMaxActive() {
        return maxActive;
    }

    public Integer getMinIdle() {
        return minIdle;
    }

    public Integer getInitialSize() {
        return initialSize;
    }

    public Long getMaxWait() {
        return maxWait;
    }

    public Long getTimeBetweenEvictionRunsMillis() {
        return timeBetweenEvictionRunsMillis;
    }

    public Long getMinEvictableIdleTimeMillis() {
        return minEvictableIdleTimeMillis;
    }

    public Boolean getTestWhileIdle() {
        return testWhileIdle;
    }

    public Boolean getTestOnBorrow() {
        return testOnBorrow;
    }

    public Boolean getTestOnReturn() {
        return testOnReturn;
    }

    public String getValidationQuery() {
        return validationQuery;
    }

    public Boolean getPoolPreparedStatements() {
        return poolPreparedStatements;
    }

    public Integer getMaxPoolPreparedStatementPerConnectionSize() {
        return maxPoolPreparedStatementPerConnectionSize;
    }

    //初始化方法，根据数据库类型，生成驱动类名和连接url
    private void init() {
        if (this.databaseType.equals("oracle")) {
            this.driverClassName = "oracle.jdbc.OracleDriver";
            this.url = "jdbc:oracle:thin:@" + this.host + ":" + this.port + "/" + this.databaseName;
            this.validationQuery = "select 1 from dual";
        }

        switch (this.databaseType) {
            case "mysql":
			    // MySQL 8.0 以下版本
                this.driverClassName = "com.mysql.jdbc.Driver";
                this.url = "jdbc:mysql://" + this.host + ":" + this.port + "/" + this.databaseName + "?characterEncoding=UTF-8&useSSL=false";
				// MySQL 8.0 以上版本
				// this.driverClassName = "com.mysql.cj.jdbc.Driver";
				// this.url = "jdbc:mysql://" + this.host + ":" + this.port + "/" + this.databaseName + "?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
                break;
            case "sqlserver":
                this.driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
                this.url = "jdbc:sqlserver://" + this.host + ":" + this.port + ";" + "DatabaseName=" + this.databaseName;
                break;
                default:
        }
    }

}
